from headlines.models import Channel, Article, Comment
from questions.models import Label
from users.models import User
from users.serializers.register_serializers import UserRegisterModelSerializer
from rest_framework import serializers


# 频道
class ChannelListModerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Channel
        fields = ('id', 'name')


class UserModelSerialzier(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username')


class ArticleSerialzier(serializers.Serializer):
    number = serializers.IntegerField(label='ID', read_only=True)
    title = serializers.CharField(allow_null=True, label='标题', max_length=100, required=False)
    content = serializers.CharField(label='文章内容', required=False, style={'base_template': 'textarea.html'})
    createtime = serializers.DateTimeField(label='创建日期', read_only=True)
    user = UserModelSerialzier(label='用户名称')

    class Meta:
        model = Article
        fields = '__all__'


#  文章嵌套===================================================================
class ArtiIdNameModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ('id', 'title')


class ArticUserModelSerialzier(serializers.ModelSerializer):
    articles = ArtiIdNameModelSerializer(read_only=True, many=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar', 'articles', 'fans')


class CommentSerializerItem(serializers.ModelSerializer):
    user = ArticUserModelSerialzier(read_only=True)
    subs = serializers.PrimaryKeyRelatedField(read_only=True, many=True)

    class Meta:
        model = Comment
        fields = ('id', 'content', 'article', 'user', 'parent', 'subs', 'createtime')


# 　文章
class ArticleModelsSerializer(serializers.ModelSerializer):
    user = ArticUserModelSerialzier(read_only=True)

    subs = CommentSerializerItem(read_only=True, many=True)

    class Meta:
        model = Article
        fields = '__all__'


class CommentSerializerList(serializers.ModelSerializer):
    user = ArticUserModelSerialzier(read_only=True)
    subs = CommentSerializerItem(read_only=True, many=True)

    class Meta:
        model = Comment
        fields = "__all__"


class ArticleSerializerForDetail(serializers.ModelSerializer):
    user = ArticUserModelSerialzier(read_only=True)  # 这个不写 many = True？
    comments = CommentSerializerList(read_only=True, many=True)

    class Meta:
        model = Article
        fields = "__all__"


#  文章嵌套===================================================================

# 文章列表
class ArticleSerializerForList(serializers.ModelSerializer):
    user = ArticUserModelSerialzier(read_only=True)
    collected = serializers.BooleanField(default=False)

    class Meta:
        model = Article
        fields = ("id", "title", "content", "createtime", "user", "collected_users", "collected", "image", "visits")


# 标签
class LabelMobelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = '__all__'
        # fields = ('id', 'label_name', 'desc', 'baike_url', 'label_icon')


class CommentSerializerForCreate(serializers.ModelSerializer):
    class Meta:
        model = Comment
        fields = ('id', 'content', 'article', 'user', 'parent', 'createtime')

#  ===========================文章搜索=================================

class SearchArticleSerializer(serializers.ModelSerializer):

    class Meta:
        model = Article
        fields= ('id', 'createtime', 'content', 'title')


#  ===========================文章搜索=================================
